function GetQueryString(name)
{
     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
     var r = window.location.search.substr(1).match(reg);
     if(r!=null)return  unescape(r[2]); return null;
}
var listTable = new Object();
listTable.c = GetQueryString('c');
listTable.m = "query";
listTable.divid = 'listDiv';
listTable.filter = new Object;
listTable.url = location.href.lastIndexOf("?") == -1 ? location.href.substring((location.href.lastIndexOf("/")) + 1) : location.href.substring((location.href.lastIndexOf("/")) + 1, location.href.lastIndexOf("?")) + "?c=" + listTable.c + '&';
listTable.page_type = 'page';
listTable.isIE = window.ActiveXObject ? true : false;

/**
 * 载入列表
 */
listTable.loadList = function()
{
    var url = this.url +"m="+ this.m;
    if(typeof this.filter.page == 'undefined') {
        var page = $.cookie(this.page_type);
        if(typeof page == 'undefined') {
            this.filter.page = 1;
        } else {
            this.filter.page = page;
        }
    }
    this.filter.ajax = 1;
    console.log(this.filter);
    $.post(url, this.filter, this.listCallback, 'json');
}
/**
 * 翻页
 */
listTable.showPage = function(page)
{
    this.filter.page = page;
    $.cookie(this.page_type, page);
    this.loadList();
}
/**
 * 创建一个可编辑区
 */
listTable.edit = function(obj, m, id, fields)
{
    var tag = obj.firstChild.tagName;
    if (typeof(tag) != "undefined" && tag.toLowerCase() == "input")
    {
        return;
    }

    /* 保存原始的内容 */
    var org = obj.innerHTML;
    var val = listTable.isIE ? obj.innerText : obj.textContent;

    /* 创建一个输入框 */
    var txt = document.createElement("INPUT");
    txt.value = (val == 'N/A') ? '' : val;
    txt.style.width = (obj.offsetWidth + 12) + "px" ;

    /* 隐藏对象中的内容，并将输入框加入到对象中 */
    obj.innerHTML = "";
    obj.appendChild(txt);
    txt.focus();

    /* 编辑区输入事件处理函数 */
    txt.onkeypress = function(e)
    {
        var evt = (typeof e == "undefined") ? window.event : e;
        var obj = document.all ? e.srcElement : e.target;
        if (evt.keyCode == 13)
        {
            obj.blur();
            return false;
        }

        if (evt.keyCode == 27)
        {
            obj.parentNode.innerHTML = org;
        }
    }

    /* 编辑区失去焦点的处理函数 */
    txt.onblur = function(e)
    {
        var url = listTable.url + "m=" + m;
        var params = new Object();
        params.id = id;
        if(typeof fields !== 'undefined') {
            params.fields = fields;
        }
        var val = $.trim(txt.value);
        params.val = val === '' ? '' : encodeURIComponent(val);
        params.ajax = 1;
        $.post(url, params, function(data){
            if(data.status === 0) {
                obj.innerHTML = data.msg;
            } else {
                if(typeof data.msg != 'undefined') {
                    alert(data.msg);
                }
                obj.innerHTML = org;
            }           
        }, 'json');            
    }
}

/**
 * 切换排序方式
 */
listTable.sort = function(sort_by, sort_order)
{
    if(this.filter.sort_order == sort_order) {
        this.filter.sort_order = (sort_order == "DESC") ? "ASC" : "DESC";
    } else {
        this.filter.sort_order = "DESC";
    }
    this.filter.sort_by = sort_by;    
    this.loadList();
}
/**
 * 切换状态
 */
listTable.toggle = function(obj, id, fields)
{
    var _this = $(obj);
    var val = (_this.hasClass("glyphicon-ok")) ? 0 : 1;
    var url = this.url + "m=toggle";    
    var params = new Object();
    params.id = id;
    params.val = val;
    params.fields = fields;
    params.ajax = 1;
    $.post(url, params, function(data){
        if(data.status === 0) {          
            if(val === 1) {
                $(obj).removeClass('glyphicon-remove');
                $(obj).addClass('glyphicon-ok');
            } else {
                $(obj).removeClass('glyphicon-ok');
                $(obj).addClass('glyphicon-remove');
            }
        } else {
            alert(data.msg);
        }
    }, 'json');
}
listTable.remove = function(id, info) {
    if(typeof info !== 'undefined') {
        if(!confirm(info)) {
            return;
        }
    } else {
        if(!confirm("真的删除吗?")) {
            return;
        }
    }
    
    var url = this.url + "m=remove";
    var params = new Object();
    params.id = id;
    params.ajax = 1;
    $.post(url, params, function(data){
        if(data.status == 0) {
            listTable.loadList();
        } else {
            alert(data.msg);
        }
    }, "json");    
}
listTable.listCallback = function(result)
{
    console.log(result);
    if (result.status > 0)
    {
        alert(result.msg);
    }
    else
    {
        try
        {
            $("#" + listTable.divid).html(result.content);
            if (typeof result.filter == "object")
            {
                listTable.filter = result.filter;
            }
        }
        catch (e)
        {
              alert(e.message);
        }
    }
}